<?php
/** 
 * Represents a canned text entry.
 * 
 * @copyright &copy; 2006 The Open University
 * @author s.marshall@open.ac.uk
 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
 * @package resourcepage 
 */ 
class resourcepage_canned {    
    /** Database fields */
    private $canned,$html,$displayname;
    
    /**
     * Initialises from data.
     * @param string $canned Unique ID for canned text piece
     * @param string $html 
     * @param string $displayname Name referring to item for use in editing interface 
     */
    public function __construct($canned,$html,$displayname) {
        $this->canned=$canned;
        $this->html=$html;
        $this->displayname=$displayname;        
    }
    
    /** @return string String ID for canned text piece */
    public function get_canned() { return $this->canned; } 
    /** @return string HTML content of canned text */
    public function get_html() { return $this->html; } 
    /** @return string Name referring to item for use in editing interface */
    public function get_display_name() { return $this->displayname; }
    
    /**
     * Displays the content in HTML.
     * @param bool $editmode True if should include editing controls
     * @param int $cmid Module instance ID
     */
    public function display($editmode,$cmid) {
        global $CFG;
        echo '<div class="rp_canned">'.str_replace('%%FLAGICON%%',
            $CFG->pixpath.'/mod/resourcepage/pix/resourcepage_flags',$this->html);
        if($editmode) {
            $pixpath=$CFG->pixpath;
            $deletealt=get_string('delete');
            print " 
<form method='post' action='cannedusage.php'><div>
<input type='hidden' name='action' value='delete' />
<input type='hidden' name='scrollpos' value='' />
<input type='hidden' name='sesskey' value='".sesskey()."' />
<input type='hidden' name='id' value='$cmid' />
<input type='hidden' name='canned' value='{$this->canned}' />
<input class='rp_img' name='actiondelete' type='image' src='$pixpath/t/delete.gif' alt='$deletealt' />
</div></form>
                ";
        }
        echo '</div>';        
    }
    
    /**
     * Displays an add button for adding this item.
     * @param int $cmid Module instance ID
     * @param int $position Position (0=top, 1=bottom) 
     */
    public function display_add_button($cmid,$position) {
        print "<form method='post' action='cannedusage.php'><div>".
"<input type='hidden' name='action' value='add' />".
"<input type='hidden' name='scrollpos' value='' />".
"<input type='hidden' name='sesskey' value='".sesskey()."' />".
"<input type='hidden' name='id' value='$cmid' />".
"<input type='hidden' name='canned' value='{$this->canned}' />".
"<input type='hidden' name='position' value='$position' />".
"<input class='rp_cannedsubmit' type='submit' value='{$this->displayname}'/>".
"</div></form>";
        
    }
     
    /**
     * Displays canned items and/or add buttons at top and bottom of page
     * @param bool $editing True if should display at all
     * @param int $cmid Course-modules ID
     * @param int $resourcepageid Resourcepage ID
     * @param object &$cannedinfo Result of module_resourcepage::get_canned_info
     * @param int $position 0 for top, 1 for bottom
     */
    public static function display_position($editing,$cmid,$resourcepageid,&$cannedinfo,$position) {
        
        // Display items along with gap b
        $thislist = $position==1 ? $cannedinfo->bottom : $cannedinfo->top;
        
        if(count($thislist)==0 && !$editing) {
            return;
        }
        
        print '<div class="rp_canneditems '.($position?'rp_cannedbottom':'rp_cannedtop').'">';
        foreach($thislist as $canned) {
            $canned->display($editing,$cmid);
        }        
        print '</div>';
        if(!$editing) {
            return;
        }
                
        // Display add buttons
        if(count($cannedinfo->unused)>0) {
            print '<div class="rp_cannedbuttons">'.get_string('addcanned','resourcepage').' ';
            foreach($cannedinfo->unused as $canned) {
                $canned->display_add_button($cmid,$position);
            }
            print '</div>';
        }
    }
}

?>